/*
 * cal.c
 *
 *  Created on: 2016年10月26日
 *      Author: loda
 */

#include <stdio.h>

long cal(char s[]);
void push(long num);
long pop();
long tolong(char ch[]);
int isnum(char c);

#define MAX_SIZE 100

char stack[MAX_SIZE];

int index = 0;

int main(int argc, char **argv) {
    char s[] = " 1 2 - 4 5 + * ";

	long rs = cal(s);

	printf("%ld\n", rs);
}

long cal(char s[]) {
	char m[10];
	char c;
	for (int i = 0, j = 0; (c = s[i]) != '\0'; i++) {
		if (c == ' ') {
			m[j] = '\0';
			j = 0;
			push(tolong(m));
		} else if (isnum(c)) {
			m[j++] = c;
		} else if (c == '+') {
			long r = pop();
			long l = pop();
			push(l + r);
		} else if (c == '-') {
			long r = pop();
			long l = pop();
			push(l - r);
		} else if (c == '*') {
			long r = pop();
			long l = pop();
			push(l * r);
		} else if (c == '/') {
			long r = pop();
			long l = pop();
			push(l / r);
		} else {
			printf("not legal character %s", c);
			exit(-1);
		}
	}

	return pop();
}

int isnum(char c) {
	return c >= '0' && c <= '9';
}

void push(long num) {
	stack[index++] = num;
}

long pop() {
	return stack[--index];
}

long tolong(char c[]) {
	long rs = 0L;
	for (int i = 0; c[i] != '\0'; i++) {
		rs = c[i] + 10 * rs;
	}
	return rs;
}
